package org.lendoo.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.WeChatJscode2SessionLog;
import org.lendoo.web.enumeration.ApiResultEnum;
import org.lendoo.web.pojo.common.ApiResult;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.vo.WeChatJscode2SessionLogVo;
import org.lendoo.web.util.IpAddressUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 微信认证日志的controller类
 */
@Api(value = "登录的controller类", tags = "WeChatJscode2SessionLogController")
@RestController
@RequestMapping("api/v1/weChatJscode2SessionLog")
@Slf4j
public class WeChatJscode2SessionLogController extends AbstractController {

    /**
     * 添加
     * @param weChatJscode2SessionLogVo
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("添加")
    @ApiImplicitParam(name = "weChatJscode2SessionLogDto", value = "WeChatJscode2SessionLog的dto类", required = true, dataType = "WeChatJscode2SessionLogDto")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<Boolean>> add(@RequestBody WeChatJscode2SessionLogVo weChatJscode2SessionLogVo, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == weChatJscode2SessionLogVo) {
            String message = "调用接口【/api/v1/weChatJscode2SessionLog/add时，" + ApiResultEnum.PARAMETER_EMPTY;
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        WeChatJscode2SessionLog weChatJscode2SessionLog = new WeChatJscode2SessionLog();
        BeanUtils.copyProperties(weChatJscode2SessionLogVo, weChatJscode2SessionLog);
        weChatJscode2SessionLog.setCreateTime(new Date());
        weChatJscode2SessionLog.setRemoteAddress(IpAddressUtil.getIpAddress(request));
        ServiceResult<Boolean> serviceResult = weChatJscode2SessionLogService.addWeChatJscode2SessionLog(weChatJscode2SessionLog);

        apiResult.setSuccess(serviceResult.getSuccess());
        apiResult.setCode(serviceResult.getCode());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }
}
